// pages/testnative/testnative.js
const mockChooseImage = require('./mockchooseimage').mockChooseImage
wx.enableAlertBeforeUnload({
  message: '即将退出当前页面',
})
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scopes: [
      'userLocation',
      'userLocationBackground',
      'werun',
      'record',
      'writePhotosAlbum',
      'camera',
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    mockChooseImage().then(d => console.log("after mock", d))
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  testModalNoTitle() { 
    wx.showModal({
      content: "modal content",
      success(res) {console.log("modal success", res)},
    })
  },
  testModal() { 
    wx.showModal({
      title: 'test modal',
      content: 'modal content',
      success(res) {
        console.log('modal success', res);
      },
    });
  },
  testActionSheet() {
    wx.showActionSheet({
      itemList: ['A', '测试', 'C'],
      success(res) {
        console.log(res);
      },
      fail(err) {
        console.error(err);
      },
    });
  },
  testAuth(e) {
    wx.authorize({
      scope: e.target.dataset.scope,
      success(res) {
        console.log(res);
      },
      fail(err) {
        console.error(err);
      },
    });
  },
  testLocationUpdate() {
    console.log("testLocationUpdate")
    console.log(wx.onLocationChange.toString())
    wx.stopLocationUpdate({
      success: (res) => {},
    })
    wx.onLocationChangeError((result) => {console.log("onLocationChangeError", result)})
    wx.onLocationChange((result) => {
      console.log("onLocationChange", result);
      wx.offLocationChangeError(() => {
        console.log("offLocationChangeError")
      })
      wx.offLocationChange((result) => {console.log("offLocationChange", result)});
      wx.stopLocationUpdate({
      success: (res) => {},
    })})
    wx.startLocationUpdate({
      success: (res) => {},
    })
  },
  testGetLocation() {
    wx.getLocation({
      type: "wgs84",
      success(res) {
        console.log("wgs84", res);
      },
      fail(err) {
        console.error(err);
      },
    });
    wx.getLocation({
      type: "gcj02",
      success(res) {
        console.log("gcj02", res);
      },
      fail(err) {
        console.error(err);
      },
    });
  },
  testGetLocation2() {
    wx.getLocation({
      success(res) {
        wx.showModal({
          content: JSON.stringify(res),
        })
      },
      fail(err) {
        console.error(err)
      }
    })
  },
  testChooseLocation() {
    wx.chooseLocation({
      success(res) {
        console.log(res);
      },
      fail(err) {
        console.error(err);
      },
    });
  },

  testChooseAddress() {
    wx.chooseAddress({
      success(res) {
        console.log(res)
        console.log(JSON.stringify(res))
      },
      fail(err) {
        console.error(err)
      }
    })
  },

  testChooseInvoice() {
    wx.chooseInvoice({
      success(res) {
        console.log(res)
      },
      fail(err) {
        console.error(err)
      }
    })
  },
  testGetWeRunData() {
    wx.getWeRunData({
      success(res) {
        console.log(res);
      },
      fail(err) {
        console.error(err);
      },
    });
  },
  getUserProfile() {
    wx.getUserProfile({
      desc: '我要拿你的信息',
      complete(res) {
        console.log(res);
      },
    });
  },
  testGetPhoneNumber(res) {
    console.log(res.detail);
  },
  testGetRealtimePhoneNumber(res) {
    console.log("testGetRealtimePhoneNumber", res.detail)
  },
  chooseMedia(res) {
    wx.chooseMedia({
      success: console.log,
      fail: console.log,
    });
  },
  chooseImage(res) {
    wx.chooseImage({
      success: console.log,
      fail: console.log
    })
  },
  previewImage() {wx.chooseImage({
    success: res => wx.previewImage({
      urls: res.tempFilePaths,
    }),
    fail: console.log
  })},
  requestSubscribeMessage() {
    const self = this;
    wx.requestSubscribeMessage({
      tmplIds: ['rkq3ExuadAT_gBTK1J19sRwh67vq3Hqe9xxE0aAT9jE'],
      success(res) {
        console.log(res);
        if (res.errMsg === 'requestSubscribeMessage:ok') {
          console.log('apply ok');
        }
      },
      complete(res) {
        console.log(res);
      },
    });
  },
  getstorage(e) {
    console.log(wx.getStorageInfoSync())
    console.log(wx.getStorageSync('openId'))
  },
  bindGetUserInfo(e) {
    console.log(e)
  },
  formSubmit(e) {
    console.log("formSubmit", e.detail.value)
    this.setData({
      user: e.detail.value.user
    })
  }
})
